$(document).ready(function() {


//Nav Toggle
	$(".nav-toggle").on('click', function (e) {
	        $(this).toggleClass('active');
	        $($(this).attr('href')).slideToggle(500,'easeInOutQuint');
	        return false;
    });




//Mainnav Toggle
	$(".mainnav-toggle").on('click', function (e) {
	        $(this).toggleClass('active');
	        $($(this).attr('href')).toggleClass("mobile-active");
	        $('body').toggleClass("nav-active");
	        return false;
    });




//Nav Toggle
	$(".mainnav a").on('click', function (e) {
	        $('.mainnav').removeClass("mobile-active");
	        $('body').removeClass("nav-active");
    });



	var applyTabs = function (container){

		// Tab switching
		var $tabs = $("li",container),
			hideTabs = function (){
				$tabs.each(function(index,element){
					$(tabTarget($(element))).hide();
				});
			},
			tabTarget = function ($tab){
				return document.getElementById($tab.find("a").attr("href").trim().replace('#',''));
			},
			init = function(){
				hideTabs();
				$tabs.first().addClass("active").show();
				$( tabTarget($tabs.first())).show();
			};

		$tabs.click(function() {
			var $this = $(this);

			$tabs.removeClass("active");
			$this.addClass("active");

			hideTabs();

			var activeTab = tabTarget($this);
			$(activeTab).fadeIn();

			return false;
		});

		init();
	},



	//wine selector
	applyWineSwiper = function (){
		//set up some state
		var swiperContainer = document.getElementById('wine-swiper'),
		winePlaceholderContainer = document.getElementById('wine-place-holder-container'),
		wineSliderContainer = document.getElementById('wine-slider-container'),
		$wineSelectorTabs = $(".tabs.winetype-toggle li",wineSliderContainer),
		$wineSelectorPanels = $(".paginated",wineSliderContainer);
		swiper = null,
		//function to reinit the swiper and move the content about upon selection
		reInitSwiper = function(node){

			var el = swiperContainer.getElementsByTagName('ul');

			for(var i=0,c = el.length; i < c; i++){
				var n = el[i];
				swiperContainer.removeChild(n);
				n.className = 'wine-placeholder';
				winePlaceholderContainer.appendChild(n);
			}

			winePlaceholderContainer.removeChild(node);
			node.className = 'nav swiper-wrapper';
			swiperContainer.appendChild(node);

			if (null == swiper){
				swiper = new Swiper(swiperContainer,{
					paginationClickable: true,
					slidesPerView: 'auto'
				});
			} else {
				setTimeout(function () {
					swiper = new Swiper(swiperContainer,{
						paginationClickable: true,
						slidesPerView: 'auto'
					});
				}, 500);
			}
		},
		//do the initial setup
		init = function (){
			$wineSelectorPanels.hide();
			$wineSelectorTabs.first().addClass("active").show();
			$wineSelectorPanels.first().show();
			reInitSwiper(winePlaceholderContainer.getElementsByTagName('ul')[0]);
			
			$('.arrow-left').on('click', function(e){
			  e.preventDefault()
			  swiper.swipePrev()
			});
			
			$('.arrow-right').on('click', function(e){
			  e.preventDefault()
			  swiper.swipeNext()
			});
		};

		//event handler, hide visible, swap the content, show it again
		$wineSelectorTabs.on('click',function (e){

			$wineSelectorTabs.removeClass("active");
			$(this).addClass("active");
			$wineSelectorPanels.hide();

			var activeTabId = $(this).find("a").attr("href").trim().replace('#',''),
			activeTab = document.getElementById(activeTabId);

			reInitSwiper(activeTab);

			$wineSelectorPanels.fadeIn();

			return false;
		});

		init();

	};

	$("#wine-swiper").each(function (){
		applyWineSwiper();
	});

	$("body.page-distributors .tabs").each(function (index,element){
		applyTabs(element);
	});

//attach tabs in a given page/container..

//Subnav collapse
	if ($(window).width() <= 800){
		$(".subnav a").on('click', function (e) {
			$('.subnav').slideToggle(500,'easeInOutQuint');
		});
	}




// Smooth anchor scroll
	$('.mainnav a').click(function(){
	    $('html, body').animate({
	        scrollTop: $( $.attr(this, 'href') ).offset().top - 0,
	    }, 1200, 'easeInOutQuint');
	    return false;
	});

	$(window).scroll(function() {
	    //This gives the scroll position
	    var scrollTop = $(window).scrollTop();
	    if(scrollTop - 940) {
	    	$('.anchor').removeClass('active');
	    }
	    if(scrollTop >= 940) {
	    	$('.anchor').addClass('active');
	    }
	});




// Anchor nav
	$('.anchor-nav a').click(function(){
	    $('html, body').animate({
	        scrollTop: $( $.attr(this, 'href') ).offset().top - 58,
	    }, 1200, 'easeInOutQuint');
	    return false;
	});




// Our People section
	$('.people-nav a').click(function(){
		$('.people-nav a').removeClass("active"); //Add "active" class to selected tab
        $(".people-nav .info").slideUp(1000,'easeInOutQuint');
        $($(this).attr('href')).slideDown(800,'easeInOutQuint');
		$(this).toggleClass("active"); //Add "active" class to selected tab
	    $('html, body').animate({
	        scrollTop: $(this).offset().top - 55,
	    }, 1200, 'easeInOutQuint');
	    return false;
	});
	$('.people-nav .close').click(function(){
        $('.people-nav .info').slideUp(1000,'easeInOutQuint');
		$('.people-nav a').removeClass("active"); //Add "active" class to selected tab
	    return false;
	});


// Proportional iframe embeds
	$('iframe,embed,object').each(function(){
		$(this).removeAttr('width');
		$(this).removeAttr('height');
		$(this).wrap('<div class="video-container">');
	});
	
	

// Proportional iframe embeds
	$('iframe,embed,object').each(function(){
		$(this).removeAttr('width');
		$(this).removeAttr('height');
		$(this).wrap('<div class="video-container">');
	});
	
	
	// Fancybox
	$('.modal').fancybox({
		margin: 0,
		padding: 0,
		width: 900,
		helpers: {
			title: null
		}
	});

// Fancybox image
	$(".modal-img").fancybox({
		openEffect	: 'elastic',
		closeEffect	: 'elastic',

		type : 'image',
		padding: 0,
		maxHeight: '95%',
		maxWidth: '95%',
		helpers: {
			title : {
				type : 'outside'
			},
			overlay : {
	            css : {
	                'background' : 'rgba(0,0,0, 0.5)'
	            }
	        }
		}
	});




//Custom scrollbars
	$(".scroll-area").mCustomScrollbar({
		scrollInertia:150
	});


// Contact form validate
	$(".contact-form").validate();





});











// Google map
	function initialize(zoomvalue) {
	var address = "550 State Highway 16, Kumeu,	New Zealand";
	geocoder = new google.maps.Geocoder();

	geocoder.geocode( { 'address': address}, function(results, status) {
	   if (status == google.maps.GeocoderStatus.OK) {
	  	var myLatlng = results[0].geometry.location;
	  	var myCountryname = results[0].geometry.location;
	  	ll = myLatlng;
	  	var myOptions = {
			   zoom: zoomvalue,
			   scrollwheel: false,
			   center: myLatlng,
	 		   mapTypeId: google.maps.MapTypeId.ROADMAP

	  	}
			var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
				//map.setUIToDefault();
				//map.setMapType(G_HYBRID_MAP);
			map.setOptions({
				styles: [  { 'featureType': 'water', 'stylers': [{ 'visibility': 'on' },{ 'color': '#acbcc9' } ]  },{ 'featureType': 'landscape', 'stylers': [{ 'color': '#f2e5d4' } ]  },{ 'featureType': 'road.highway', 'elementType': 'geometry', 'stylers': [{ 'color': '#c5c6c6' } ]  },{ 'featureType': 'road.arterial', 'elementType': 'geometry', 'stylers': [{ 'color': '#e4d7c6' } ]  },{ 'featureType': 'road.local', 'elementType': 'geometry', 'stylers': [{ 'color': '#fbfaf7' } ]  },{ 'featureType': 'poi.park', 'elementType': 'geometry', 'stylers': [{ 'color': '#c5dac6' } ]  },{ 'featureType': 'administrative', 'stylers': [{ 'visibility': 'on' },{ 'lightness': 33 } ]  },{ 'featureType': 'road'  },{ 'featureType': 'poi.park', 'elementType': 'labels', 'stylers': [{ 'visibility': 'on' },{ 'lightness': 20 } ]  },{  },{ 'featureType': 'road', 'stylers': [{ 'lightness': 20 } ]  }],

				// mapTypeId: google.maps.MapTypeId.SATELLITE
			});


		    var image = '/images/map-marker.png';
		    var beachMarker = new google.maps.Marker({
		    	position: myLatlng,
		        map: map,
		        icon: image
	  	});
	   } else {
	      document.getElementById('map_canvas').innerHTML='Map currently not available.';
	   }
	});
}
